package com.pirestupppai.imagetool;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

/**
 *  自定义拖动text
 */

public class CustomTextModel {
    public Paint textPaint;
    public String textString = "测试";
    public float textSize = 40;
    public int textColor = Color.BLACK;
    public float textMarginLeft = 0f;      // 距离父控件左边的距离
    public float textMarginTop = 0f;       // 距离父控件上边的距离，注意坐标原点在左上角，所以，textMarginTop在下面
    public float textMarginRight = 0f;
    public float textMarginBottom = 0f;
    public Rect bounds;                    // 边界矩阵，能够获取Text的宽度和高度
    public float startX = 0f;              // 开始拖动前，距离屏幕左边的距离
    public float startY = 0f;              // 开始拖动前，距离屏幕上边的距离

    public float startTouchX = 0f;         // 触摸点的X位置
    public float startTouchY = 0f;         // 触摸点的Y位置

    private int fatherWidget;              // 父控件的宽度
    private int fatherHeight;              // 父控件的高度

    public int textStyle = 0;

    public CustomTextModel(int fatherWidget, int fatherHeight) {
        textPaint = new Paint();
        textPaint.setStrokeWidth(3);
        textPaint.setTextSize(textSize);
        textPaint.setColor(textColor);
        textPaint.setTextAlign(Paint.Align.LEFT);
        textPaint.setAntiAlias(true);
        bounds = new Rect();
        this.fatherWidget = fatherWidget;
        this.fatherHeight = fatherHeight;

        textPaint.getTextBounds(textString, 0, textString.length(), bounds);
        textMarginLeft = fatherWidget/2 - bounds.width()/2;
        textMarginTop = fatherHeight/2 + bounds.height()/2;
        textMarginRight= textMarginLeft + bounds.width();
        textMarginBottom = textMarginTop - bounds.height();

        startX = textMarginLeft;
        startY = textMarginTop;

    }

    /**
     *  文字内容改变后，view的长度会发生变化，需要设置textMarginRight
     */
    public void SetTextContent() {
        textPaint.getTextBounds(textString, 0, textString.length(), bounds);
        textMarginRight= textMarginLeft + bounds.width();
    }

    /**
     *   设置文字的大小
     */
    public void SetTextSize(float textSize) {
        this.textSize = textSize;
        textPaint.setTextSize(textSize);
        textPaint.getTextBounds(textString, 0, textString.length(), bounds);

        textMarginRight= textMarginLeft + bounds.width();
        textMarginBottom = textMarginTop - bounds.height();
    }

    /**
     *  设置文字的颜色
     */
    public void SetTextColor(int color) {
        this.textColor = color;
        textPaint.setColor(textColor);
    }

}
